package com.example.szh.splash.viewmodel;

import com.blankj.utilcode.util.LogUtils;
import com.example.mvvmcommon.field.UIField;
import com.example.mvvmcommon.mvvm.viewmodel.BaseViewModel;
import com.example.networkmoudel.entity.BaseEntity;
import com.example.networkmoudel.utils.SpUtils;
import com.example.szh.fragment.view.FragmentActivity;
import com.example.szh.splash.data.TokenEntity;
import com.example.szh.splash.model.SplashModel;
import com.example.szh.splash.timer.handler.TimerHandler;
import com.example.szh.splash.timer.thread.TimerThread;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class SplashViewModel extends BaseViewModel<SplashModel> implements TimerHandler.TimerCallBack{


    @Override
    protected void initData() {
        Map<String,Object>map=new HashMap<>();
        map.put("imie", ""+System.currentTimeMillis());
        m.requestToken(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    protected SplashModel createModel() {
        return new SplashModel();
    }

    @Override
    protected void sucessFull(BaseEntity entity) {
        if (entity instanceof TokenEntity){
            TokenEntity entity1 = (TokenEntity) entity;
            LogUtils.json(entity1);
            SpUtils.saveData("token",entity1.getValues().token);
            new TimerThread(new TimerHandler(this)).start();
        }
    }

    @Override
    public void callBack() {
        Map<String,Object> map=new HashMap<>();
        map.put(UIField.ACTIONTYPE,UIField.NATIVESTART);
        map.put(UIField.NATIVEACTION, FragmentActivity.class);
        getUiChangeLiveData().setStartActivity(map);
        getUiChangeLiveData().getFinishActivity().setValue(""+System.currentTimeMillis());
    }
}
